package tester;

/**
 * Wrapper for reference to an Examples class, by package name and class name.
 * 
 * @author Shayne Caswell
 */
public class ExampleWrapper implements Comparable<ExampleWrapper>{
	
	/** The package name. */
	private String packageName;
	
	/** The class name. */
	private String className;
	
	/**
	 * Default constructor, always throws an exception.
	 * 
	 * This class should always specify a package and a class.
	 */
	public ExampleWrapper(){
		throw new IllegalArgumentException("No package or classname specified");
	}
	
	/**
	 * Instantiates a new example wrapper.
	 * 
	 * <code>ExampleWrapper</code> objects must always specify both packageName
	 * and className. An empty string denotes one of two things:
	 * - if used for the packageName, the given className is in the default package.
	 * - if used for the className, the given package should be searched for Examples classes.
	 * 
	 * One of these fields should not be an empty string.
	 * 
	 * @param packageName Name of the package the class is contained in
	 * @param className Name of the class to be used as an Examples class
	 */
	public ExampleWrapper(String packageName, String className){
		this.packageName = packageName;
		this.className = className;
	}
	
	/**
	 * Gets this ExampleWrapper object's Examples class package name
	 * 
	 * @return the name of the package for Examples class
	 */
	public String getPackageName(){
	    return this.packageName;
	}
	
	/**
	 * Gets this ExampleWrapper object's Examples class name
	 * 
	 * @return the name of the class of this Examples class
	 */
	public String getClassName(){
	    return this.className;
	}

	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((className == null) ? 0 : className.hashCode());
		result = prime * result
				+ ((packageName == null) ? 0 : packageName.hashCode());
		return result;
	}

	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ExampleWrapper other = (ExampleWrapper) obj;
		if (className == null) {
			if (other.className != null)
				return false;
		} else if (!className.equals(other.className))
			return false;
		if (packageName == null) {
			if (other.packageName != null)
				return false;
		} else if (!packageName.equals(other.packageName))
			return false;
		return true;
	}

	public int compareTo(ExampleWrapper ex) {
		if(this.equals(ex)){
			return 0;
		} else{
			if(this.packageName.compareTo(ex.getPackageName()) != 0){
			    return this.packageName.compareTo(ex.getPackageName());
			}else{
			    return this.className.compareTo(ex.getClassName());
			}
		}
	}
}